package algorithm.thread_demo;

/**
 * Thread join
 *按照递增的顺序展示出来
 */
public class ThreadJoinDemo extends Thread{
    int i;
    Thread preThread;//上一个线程
    public ThreadJoinDemo(Thread preThread, int i) {
        this.preThread = preThread;
        this.i = i;
    }

    @Override
    public void run() {
        //调用上一个线程的join方法，大家可以自己演示的时候可以把这行代码注释掉
        try {
            preThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("num:"+i);
    }

    public static void main(String[] args) {
        Thread preThread = Thread.currentThread();
        for(int i=0;i<10;i++){
            ThreadJoinDemo threadJoinDemo = new ThreadJoinDemo(preThread,i);
            threadJoinDemo.start();
            preThread = threadJoinDemo;
        }
    }
}
